//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var manual_id=thisform.find('input[name=manual_id]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var cloud_depot_status=thisform.find('input[name=cloud_depot_status]').val();
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		var time_cate=thisform.find('input[name=time_cate]').val();
		// console.log(inbound_status);
		// return false;
		$('#moveStorageDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#moveStorageDatagrid').datagrid({ 
			queryParams:{
				status:1,//已审核通过
				order_no:order_no,
				grounding_status:1,//未上架
				manual_id:manual_id,
				goods_no:goods_no,
				goods_name:goods_name,
				inbound_status:1,//已入库
				cloud_depot_status:cloud_depot_status,
				start_time:start_time,
				end_time:end_time,
				time_cate:time_cate
			}
		});
	})
	//更新行数据
	function updateActions(value){
		$.post(confirmMoveStorageUrl,{id:value.id,storage_code:value.storage_code},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#moveStorageDatagrid').datagrid('validateRow', editIndex)){
			$('#moveStorageDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//单击行
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#moveStorageDatagrid').datagrid('selectRow', index);
				$('#moveStorageDatagrid').datagrid('endEdit', editIndex);
				$('#moveStorageDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#moveStorageDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	
	$('#moveStorageDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getarrivalUrl,
		title:'库存明细',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		queryParams:{
			status:1,
			inbound_status: 1,
			grounding_status:1//未上架
		},
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'manual_id',title:'账册编号'},
		{field:'goods_no',title:'商品编码'},
		{field:'goods_name',title:'商品名称'},
		{field:'goods_spec',title:'规格'},
		{field:'receive_amount',title:'实际到货数量',sortable:true},
		{field:'bar_code',title:'条形码'},
		{field:'storage_code',title:'库位',sortable:true,width:'100px',
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'name',
					method:'get',
					url:storageListUrl,
					required:true,
					validType:['selected[]']
				}
			}
		},
		{field:'cloud_depot_status',title:'是否云仓商品',sortable:true},
		{field:'cloud_num',title:'云仓商品数量',sortable:true},
		{field:'cloud_unit',title:'云仓商品单位',sortable:true},
		{field:'conforming_p',title:'合格品',sortable:true},
		{field:'nonconforming_p',title:'残次品',sortable:true},
		{field:'scrap_one',title:'报废品',sortable:true},
		// {field:'push_grounding_time',title:'推送上架时间',sortable:true},
		// {field:'push_grounding_times',title:'推送上架次数',sortable:true},
		{field:'expected_time',title:'预计到货时间',sortable:true},
		{field:'arrival_time',title:'到货时间',sortable:true},
		// {field:'push_arrival_time',title:'推送实收时间',sortable:true},
		// {field:'examine_time',title:'审核时间',sortable:true}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		pagination:true,
		fit:true,
	});
});
moveStorageObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#moveStorageDatagrid').datagrid('validateRow', editIndex)){
			$('#moveStorageDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#moveStorageDatagrid').datagrid('acceptChanges');
			$('#moveStorageDatagrid').datagrid('reload');
		}
	},
	save:function(url){
		$('#moveStorageEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#moveStorageEditDialog').dialog('close');
                    });
                }else{
                    $('#moveStorageEditDialog').dialog('close');      
                    $('#moveStorageDatagrid').datagrid('reload');    
                }
            }
        });
	},
	moveStorageBar:function(){
		var moveStorageRow=$('#moveStorageDatagrid').datagrid('getSelections');
        if (moveStorageRow.length>1){		            	
            $.messager.alert('提示','一次只能上架一条记录!','info');
        }else if(moveStorageRow.length==1){            	
        	$('#moveStorageEditDialog').dialog('open').dialog('setTitle','商品移库');
            $('#moveStorageEditForm').form('load',moveStorageRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	showAll:function(){
		$('#moveStorageDatagrid').datagrid({ queryParams:{status:1,grounding_status:1,inbound_status:1}});//已入库参数
	},
	infoBar:function(){//移库记录
		window.parent.showTab(moveStoragelistUrl,'移库记录');
	},
	reloadBar:function(){
		$('#moveStorageDatagrid').datagrid('reload');
	}

}